import Vue from "vue"
import {
	httpApi
} from '../config'


const BASE_URL = httpApi + 'api/'

/* 
 
	@parems 
	@parems {Boolean} loading	默认：false		是否显示loading弹窗
	@parems	{Boolean}	noAuth	默认：true 		是否需要登录状态
 
 */

// 封装get方法

const get = (options) => {
	return new Promise((resolve, reject) => {
		if (!options.noLoading){
			uni.showLoading({
				title: '加载中..'
			})
		}
		try {
			uni.request({
				url: (options.BASE_URL || BASE_URL) + options.url,
				method: 'GET',
				data: options.data,
				timeout: 200000,
				header: {
					'token': uni.getStorageSync('token'),
					'content-Type': 'application/x-www-form-urlencoded'
				},
				success: res => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					if (res.statusCode == 200) {
						resolve(res.data)
					}
					if (res.statusCode == 401 && !options.noAuth) {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						})
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('token')
						uni.switchTab({
							url: '/pages/user/user'
						})
					}
					if (res.statusCode == 500) {
						uni.showToast({
							title: '系统错误，请稍后重试',
							icon: 'none'
						})
					}
				},
				fail: err => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					reject(err.data)
				},
				complete: () => {}
			})
		} catch (e) {
			uni.stopPullDownRefresh()
			uni.hideLoading()
			uni.showToast({
				title: '服务器或网络异常，请稍候重试',
				icon: 'none'
			})
		}
	})
}

// 封装post方法
const post = (options) => {
	return new Promise((resolve, reject) => {
		if (!options.noLoading){
			uni.showLoading({
				title: '加载中..'
			})
		}
		try {
			uni.request({
				url: (options.BASE_URL || BASE_URL) + options.url,
				method: 'POST',
				data: options.data,
				header: {
					'token': uni.getStorageSync('token'),
					'content-Type': 'application/json',
				},
				success: res => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					if (res.statusCode == 200) {
						console.log(res.data)
						if (res.data.code == 0) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
						resolve(res.data)
					}
					if (res.statusCode == 401 && !options.noAuth) {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						})
						uni.removeStorageSync('userInfo')
						uni.removeStorageSync('token')
						uni.switchTab({
							url: '/pages/user/user'
						})
					}
					if (res.statusCode == 500) {
						uni.showToast({
							title: '系统错误，请稍后重试',
							icon: 'none'
						})
					}
				},
				fail: err => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
					reject(err.data)
				},
				complete: () => {
					uni.stopPullDownRefresh()
					uni.hideLoading()
				}
			})
		} catch (e) {
			uni.stopPullDownRefresh()
			uni.hideLoading()
			uni.showToast({
				title: '服务器或网络异常，请稍候重试',
				icon: 'none'
			})
		}
	})
}

export default {
	get,
	post,
	BASE_URL
}
